﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.EventSystems;

public class Block : MonoBehaviour
{
	public SpriteRenderer render;
	int _id;
	public TextMeshPro text;
	public Vector3 offset;

	public int id {
		get { return _id; }
		set {
			_id = value;

		}
	}

	int _colorId;

	public int colorId {
		get { return _colorId; }
		set {
			_colorId = value;
			//render.color = GetColorByID (value);
			render.sprite = GameDefine.instance.listBlockSprites [value];
		}
	}

	Color GetColorByID (int id)
	{
		switch (id) {
		case 5:
			return Color.white;
		case 4:
			return Color.red;
		case 1:
			return Color.blue;
		case 0: 
			return Color.black;
		case 3: 
			return Color.yellow;
		case 2: 
			return Color.green;
		default :
			return Color.black;
		}

	}

	int _x;

	public int x {
		get { return _x; }
		set { 
			_x = value;
			text.text = x + ":" + y;
			gameObject.name = "Block_" + x + "_" + y;
		}
	}

	int _y;

	public int y {
		get { return _y; }
		set { 
			_y = value;
			text.text = x + ":" + y;
			gameObject.name = "Block_" + x + "_" + y;
		}
	}


	void OnMouseDown ()
	{
		#if !UNITY_EDITOR
		if ((Input.touchCount > 0 && EventSystem.current.IsPointerOverGameObject (Input.GetTouch (0).fingerId)) || GameManager.gameState != GameState.PLAYING ) {
			return;
		}
		#endif
		if (x < 6 || x >= 12 || y < 6 || y >= 12) {
			return;
		}
		if (GameplayControl.instance.canClickMove == true && GameManager.gameState == GameState.PLAYING) {
			GameplayControl.instance.InitMove (this);
			GameplayControl.instance.canClickMove = false;
		}
	}
}
